<template>
    <div class="publicpageview">
        <div class="publicformview">
            <el-form :inline="true" :model="listQuery" size="mini" ref="searchForm">
                <el-row :gutter="20">
                    <el-col :span="4">
                        <el-form-item label="账户名称：" prop="keyword">
                            <el-input v-model="listQuery.keyword"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <el-form-item label="账户类型：" prop="status">
                            <el-select v-model="listQuery.status" clearable>
                                <el-option label="启用" value="0" key="1"></el-option>
                                <el-option label="禁用" value="1" key="0"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <div>
                            <el-button size="mini" type="primary" @click="handleSearch">查询</el-button>
                            <el-button size="mini" @click="resetForm('searchForm')">重置</el-button>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <div class="publitableview">
            <el-button type="primary" size="mini" @click="handleAddUser('add')">新增账户</el-button>
            <el-table :data="list" border fit highlight-current-row size="mini">
                <el-table-column align="center" label="账户名称" prop="id">
                </el-table-column>
                <el-table-column align="center" label="账号" prop="name">
                </el-table-column>

                <el-table-column align="center" label="类型" prop="status">
                    <template slot-scope="scope">
                        <span>{{ scope.row.status == 0 ? "禁用" : "启用" }}</span>
                    </template>
                </el-table-column>

                <el-table-column align="center" label="期初余额" prop="name">
                </el-table-column>

                <el-table-column align="center" label="启用日期" prop="name">
                </el-table-column>

                <el-table-column align="center" label="账户状态" prop="name">
                </el-table-column>

                <el-table-column align="center" label="备注" prop="name">
                </el-table-column>
                <el-table-column align="center" label="操作" width="180">
                    <template slot-scope="scope">
                        <el-button type="text" size="mini" @click="handleUpdate(scope.row, 'update')">修改</el-button>
                        <el-button type="text" size="mini" @click="handleUpdate(scope.row,'details')">详情</el-button>
                        <el-button type="text" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="publicpaginationview">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="listQuery.pageNum" background :page-sizes="[10, 20, 50, 100]"
                    :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
                    small></el-pagination>
            </div>
        </div>

        <el-dialog :title="isAdd == 'add' ? '新增账户' : isAdd == 'update' ? '修改账户' :'账户详情'" :visible.sync="dialogVisible"
            width="40%" :before-close="handleClose">
            <el-form :model="formData" ref="dataForm" class="demo-ruleForm" label-position="right" label-width="120px"
                size="mini">
                <el-form-item label="账户名称：" prop="id">
                    <el-input v-model="formData.id"></el-input>
                </el-form-item>
                <el-form-item label="账号：" prop="name">
                    <el-input v-model="formData.name"></el-input>
                </el-form-item>


                <el-form-item label="类型：" prop="status">
                    <el-select v-model="formData.status">
                        <el-option label="启用" value="1" key="1"></el-option>
                        <el-option label="禁用" value="0" key="0"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="期初余额：" prop="name">
                    <el-input v-model="formData.name"></el-input>
                </el-form-item>

                <el-form-item label="启用日期：" prop="name">
                    <el-date-picker v-model="formData.projectYear" type="date" placeholder="年/月/日">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="账户状态：" prop="status">
                    <el-select v-model="formData.status">
                        <el-option label="启用" value="1" key="1"></el-option>
                        <el-option label="禁用" value="0" key="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="备注：" prop="remarks">
                    <el-input v-model="formData.remarks" type="textarea" :rows="2"></el-input>
                </el-form-item>
            </el-form>



            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleClose" size="medium" v-if="isAdd == 'details'">返
                    回</el-button>

                <el-button type="primary" @click="handleSubmitClick" size="medium" v-else>保 存</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
let arr = [];
let treeArr = [];
import { listAll, deleteRole, createRole, allResourceRole, updateRole } from '@/api/system'
import { listResource, allocResource } from '@/api/index'
import { Message } from 'element-ui'
export default {
    name: "AccommodationManagementIndex",

    data() {
        return {
            listQuery: {
                pageNum: 1,
                pageSize: 10,
                keyword: "",
                status: "",
                // phone: "",
            },


            total: 0,

            list: [

            ],


            dialogVisible: false,
            isAdd: 'add',
            formData: {

            },




        };
    },

    computed: {

    },

    mounted() {
        this.getList();




    },



    methods: {



        handleSubmitClick() {
            const { formData } = this;
            if (this.isAdd == 'add') {
                createRole({ ...formData }).then(res => {
                    this.handleClose();
                    this.getList();
                    Message({
                        message: res.message || '操作成功',
                        type: "success",
                        duration: 5 * 1000,
                    });
                })
            }
            if (this.isAdd == 'update' || this.isAdd == 'details') {
                updateRole({ ...formData }).then(res => {
                    this.handleClose();
                    this.getList();
                    Message({
                        message: res.message || '操作成功',
                        type: "success",
                        duration: 5 * 1000,
                    });
                })
            }

        },


        handleSizeChange(val) {
            this.listQuery.pageNum = 1;
            this.listQuery.pageSize = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.listQuery.pageNum = val;
            this.getList();
        },

        handleSearch() {
            this.listQuery.pageNum = 1;
            this.getList();
        },
        getList() {
            listAll(this.listQuery).then((response) => {
                this.list = response.data.list;
                this.total = response.data.total;
            });
        },



        resetForm(val) {

            this.$refs[val].resetFields();
            this.listQuery.pageNum = 1;
        },

        handleAddUser(type) {
            this.isAdd = 'add';
            this.dialogVisible = true;

        },

        handleClose() {
            this.resetForm("dataForm");
            this.dialogVisible = false;
        },

        handleUpdate(currentData, type) {
            this.isAdd = type;
            //   一定要在打开弹窗前 再赋值 不然resetFiles没用
            this.dialogVisible = true;
            this.$nextTick(() => {
                this.formData = { ...currentData, status: String(currentData.status) };

            });
        },

        handleDelete(id) {
            this.$confirm(
                "此操作将删除该条内容，请确认是否继续操作？",
                "删除提示",
                {
                    confirmButtonText: "保 存",
                    cancelButtonText: "取 消",
                    type: "warning",
                }
            )
                .then(() => {
                    deleteRole(id).then(res => {
                        Message({
                            message: res.message || '操作成功',
                            type: "success",
                            duration: 5 * 1000,
                        });
                        this.getList();
                    })
                })
                .catch(() => { });
        }
    },
};
</script>

<style scoped>
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
    width: 100%;
}
</style>